package sddz.foodmanage.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import sddz.foodmanage.R;
import sddz.foodmanage.model.CommodityBean;
import sddz.foodmanage.utils.HttpUtils;
import sddz.foodmanage.utils.ImageLoader;

/**
 * @Date:2019/7/8
 * @Author:lbb
 * 订单列表
 */

public class OrderAdapter extends BaseAdapter {
    private Context context;
    private List<CommodityBean.DataBeanX.DataBean> list;
    private ModifyCountInterface  modifyCountInterface;
    private int userid;
    public OrderAdapter(){

    }
    public void setdata(Context context,List<CommodityBean.DataBeanX.DataBean> list,int userid){
        this.context=context;
        this.list=list;
        this.userid=userid;
    }
    public OrderAdapter(Context context,List<CommodityBean.DataBeanX.DataBean> list){
        this.context=context;
        this.list=list;
    }
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface){
        this.modifyCountInterface=modifyCountInterface;
        if(this.modifyCountInterface==null){
            Log.e("modifyCountInterface","为null");
        }else{
            Log.e("modifyCountInterface","不为空");
        }
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        CommodityBean.DataBeanX.DataBean dataBean= (CommodityBean.DataBeanX.DataBean) getItem(position);
        if(convertView == null){
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.item_home_shangpin, null);
            holder = new ViewHolder();
            holder.spname = convertView.findViewById(R.id.tv_spname);
            holder.price = convertView.findViewById(R.id.tv_unitprice);
            holder.img = convertView.findViewById(R.id.image_sp);
            holder.dsx = convertView.findViewById(R.id.image_dsx);
            holder.weight = convertView.findViewById(R.id.tv_weight);
            holder.jia = convertView.findViewById(R.id.image_add);
            holder.jian = convertView.findViewById(R.id.image_jian);
            holder.num = convertView.findViewById(R.id.rl_num);
            holder.changesc = convertView.findViewById(R.id.rl_changesc);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }


        final CommodityBean.DataBeanX.DataBean info = list.get(position);
        if(info != null){

            holder.spname.setText(list.get(position).getName());
            holder.price.setText(list.get(position).getPrice() + "元/" + list.get(position).getUnit());
            holder.weight.setText(list.get(position).getNum()+"");
            if(list.get(position).getImgs().size()>0) {
                ImageLoader.loadImageUrl(context, list.get(position).getImgs().get(0).getUrl(), holder.img);
            }
            if(userid==2){
                holder.num.setVisibility(View.VISIBLE);//加减
                holder.changesc.setVisibility(View.GONE);//食材修改
            }else if(userid==1){
                holder.num.setVisibility(View.GONE);
                holder.changesc.setVisibility(View.VISIBLE);
            }
            if(list.get(position).getStatus()==1){
                holder.dsx.setVisibility(View.GONE);
            }else {
                holder.dsx.setVisibility(View.VISIBLE);
            }

            holder.jia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    modifyCountInterface.doIncrease( holder.weight,position);// 暴露增加接口
                }
            });


            holder.jian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease( holder.weight,position);// 暴露删减接口
                }
            });
            holder.weight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.doTextView(holder.weight,position);
                }
            });
            holder.changesc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.dochange(holder.changesc,position);
                }
            });
          holder.img.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  modifyCountInterface.dodatu(holder.img,position);
              }
          });

        }

        return convertView;
    }


    class ViewHolder{
        ImageView img,dsx;
        ImageView jia,jian;
        TextView spname,price;
        TextView weight;
        AutoRelativeLayout num,changesc;
    }

    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doIncrease(View showCountView,int position);

        /**
         * 删减操作
         *
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doDecrease(View showCountView,int position);
        public void doTextView(View showCountView,int position);
        public void dochange(View showCountView,int position);
        public void dodatu(View showCountView,int position);



    }
}
